let express = require('express');
let router = express.Router();
let versionDao = require('../../model/backend/version');

//获取所有版本列表

router.get('/list',async function (req,res) {
 
    let versionList  = await versionDao.versionLists();
    await res.json({status:200,msg:'success',data:versionList});
})

//添加版本

router.post('/add',async function(req,res){

let num  = req.body.num;
let content = req.body.content;
let url = req.body.url;
if(!num){
    return  await res.json({status:203,msg:'请输入版本号！'})
}
if(!content){
    return await res.json({status:203,msg:'请输入更新内容！'})
}
if(!url){
    return await res.json({status:203,msg:"请上传apk文件"})
}
//判断该版本号是否存在
 let validateNum =  await versionDao.validateNum(num);
  if(validateNum.length>0){
      return await res.json({status:203,msg:'该版本号已存在,请更换！'})
  }
  let getMaxNum  = await versionDao.getMaxNum();
     let maxNum = getMaxNum.maxNum;
     if(maxNum && maxNum>num){
        return await res.json({status:203,msg:'该版本号小于现有的版本号,请更换！'})
     }

     await versionDao.addVersion(num,content,url);

     await res.json({status:200,msg:"success"});
})


//删除已更新版本

router.post('/del',async function(req,res){
    let id = req.body.id;
    await versionDao.delVersion(id);
    await res.json({status:200,msg:"success"});

})

module.exports = router;